//
//  MasterDetailController.swift
//  BodyBuilding
//
//  Created by 14110100912 on 2017/10/3.
//  Copyright © 2017年 xsh. All rights reserved.
//

import UIKit

class MasterDetailController: UIViewController {
    var infoDict:NSDictionary!
    var videoPlayer:VideoPlayer!
    var hideState = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        viewConfig()
    }
    
    func viewConfig(){
        view.backgroundColor = UIColor.white
        
        let navBar = CustomNavBar(frame: CGRect(origin: .zero, size: CGSize(width: WIDTH, height: 64)))
        navBar.initialize(text: nil)
        view.addSubview(navBar)
        
        let imageView = UIImageView()
        imageView.sd_setImage(with: URL(string: BB_IMAGEURLPRE+(infoDict.value(forKey: "thumb") as! String)), placeholderImage: UIImage(named:"placeholder"))
        imageView.frame = CGRect(x: 0, y: 64, width: WIDTH, height: (HEIGHT-69)/3)
        view.addSubview(imageView)
        
        let label = UILabel(frame: CGRect(x: 15, y: imageView.bounds.height+74, width: WIDTH-30, height: 40))
        label.text = "视频简介"
        label.textAlignment = .center
        label.layer.cornerRadius = 5
        label.layer.borderColor = UIColor.lightGray.cgColor
        label.layer.borderWidth = 1
        view.addSubview(label)
        
        let joinBtn = CustomButton(frame: CGRect(x: 0, y: label.frame.origin.y+314, width: 150, height: 44))
        joinBtn.backgroundColor = UIColor.red
        joinBtn.center.x = WIDTH/2
        joinBtn.setTitle("关注", for: .normal)
        joinBtn.layer.cornerRadius = 8
        joinBtn.adjustsImageWhenDisabled = false
        joinBtn.isEnabled = false
        joinBtn.animation()
        view.addSubview(joinBtn)
        
        videoPlayer = VideoPlayer(frame: CGRect(x: 0, y: label.frame.origin.y+50, width: WIDTH, height: 200))
        videoPlayer.initialize(itemUrl:BB_VIDEOURL)
        view.addSubview(videoPlayer)
        
        isExits(sender: joinBtn)
        
        joinBtn.addTarget(self, action: #selector(add), for: .touchUpInside)
        
        navBar.backBtn.addTarget(self, action: #selector(back), for: .touchUpInside)
        
        videoPlayer.fullBtn.addTarget(self, action: #selector(fullScreen), for: .touchUpInside)
    }
    
    func isExits(sender:CustomButton){
        let parameters:NSDictionary = [
            "SessionID"     :   LoginModel.sessionID,
            "fan_id"        :   infoDict.value(forKey: "id") as! String,
            "userid"        :   LoginModel.userID,
            "fans_type"     :   "2",
            ]
        Networking.shared.isExits(parameter: parameters) {(success) in
            DispatchQueue.main.async {
                if success{
                    sender.setTitle("取消关注", for: .normal)
                }
                sender.isEnabled = true
                sender.stopAnimation()
            }
        }
    }

    @objc func add(sender:UIButton){
        let parameter:NSDictionary = [
            "SessionID" :   LoginModel.sessionID,
            "fan_id"    :   self.infoDict.value(forKey: "id") as! String,
            "fans_type" :   "2",
            "userid"    :   LoginModel.userID
        ]
        if sender.isEnabled{
            let popup = Popup()
            if sender.titleLabel?.text == "关注"{
                Networking.shared.focus_Join(parameter: parameter) {
                    DispatchQueue.main.async {
                        popup.appear(title: "关注成功", action: {
                            self.back()
                        })
                    }
                }
            }else{
                Networking.shared.delete(parameter: parameter, block: {
                    DispatchQueue.main.async {
                        popup.appear(title: "取消关注成功", action: {
                            self.back()
                        })
                    }
                })
            }
        }
    }
    
    @objc func fullScreen(){
        let pw = videoPlayer.bounds.width
        let ph = videoPlayer.bounds.height
        if !hideState{
            UIView.animate(withDuration: 0.3, animations: {
                self.videoPlayer.transform =
                    CGAffineTransform(rotationAngle: CGFloat(Double.pi/180)*CGFloat(90)).concatenating(
                    CGAffineTransform(scaleX: WIDTH/ph,
                                      y: HEIGHT/pw)).concatenating(
                    CGAffineTransform(translationX: 0,
                                      y: -25.833-64))
            })
            hideState = !hideState
            setNeedsStatusBarAppearanceUpdate()
        }
        else{
            UIView.animate(withDuration: 0.3, animations: {
                self.videoPlayer.transform = .identity
            })
            hideState = !hideState
            setNeedsStatusBarAppearanceUpdate()
        }
    }
    
    override var prefersStatusBarHidden: Bool{
        return hideState
    }
    
    @objc func back(){
        videoPlayer.disAppear()
        _ = navigationController?.popViewController(animated: true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
